import time
import cv2
import math
import numpy as np
import Processor.Converter as Converter

from PyQt5.QtWidgets import QDialog
from Plugin.EffectFilter.whiteBeauty_design import Ui_WhiteBeautyController


class WhiteBeautyController(QDialog, Ui_WhiteBeautyController):
    layer = None  # 图层
    mat = None  # 原始Mat 数据

    def __init__(self, parent, mat=None):
        super().__init__(parent)
        self.setupUi(self)
        self.mat = mat
        self.initUI(parent)

    def initUI(self, parent):
        # 定位
        self.resize(200, 170)
        pos = parent.geometry()
        self.move((pos.width() - self.width()) / 2, (pos.height() - self.height()) / 2)
        # 信号
        #       对话框确定
        self.DialogButtonBox.accepted.connect(self.accept)
        self.DialogButtonBox.rejected.connect(self.reject)
        # 数据
        self.spinBox.valueChanged.connect(self.updateSlot)
        self.spinBox.setMinimum(1)
        self.spinBox.setSingleStep(1)
        self.spinBox.setProperty("value", 12)
        self.spinBox.setMaximum(20)

    def reject(self):
        """
        取消操作
        :return:
        """
        image = Converter.Numpy2QImage(self.mat)
        self.layer.updatePixmap(image)
        QDialog.reject(self)

    def updateSlot(self):
        value = self.spinBox.value()
        self.blur(value)

    def blur(self, whi=12):
        """
        :param whi: 美白系数
        :return:
        """
        if not self.layer:
            return
        whi = whi / 10
        start = time.perf_counter()
        mat = self.mat.copy()
        mat = np.uint8(np.clip((whi * mat + 5), 0, 255))
        image = Converter.Numpy2QImage(mat)
        end = time.perf_counter()
        print('WhiteBeauty', '耗时：', end - start)
        self.layer.updatePixmap(image)

